package it.eng.area118.mdocommon.log;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FileLogger implements Log {

	private String fileName;

	private String completeFileName;

	private SimpleDateFormat sdf;

	private Date today;

	private SimpleDateFormat logFormat = new SimpleDateFormat("HH:mm:ss");

	private String level;

	// private DataOutputStream dos = null;

	// private ByteArrayOutputStream bos;

	private FileOutputStream stream = null;

	public FileLogger(String fileName, String level) {
		sdf = new SimpleDateFormat("yyyy-MM-dd");
		this.fileName = fileName;
		today = new Date();
		this.level = level;

	}

	public String getCompleteFileName() {
		return completeFileName;
	}

	public void info(String msg) {
		if (this.level.equals(Log.INFO) || this.level.equals(Log.DEBUG)) {
			log(msg, INFO, null);
		}

	}

	public void debug(String msg) {
		if (this.level.equals(Log.DEBUG)) {
			log(msg, DEBUG, null);
		}
	}

	public void warn(String msg, Exception e) {
		if (this.level.equals(Log.WARNING) || this.level.equals(Log.INFO)
				|| this.level.equals(Log.DEBUG)) {
			log(msg, WARNING, e);
		}

	}

	public void error(String msg, Exception e) {
		if (this.level.equals(Log.INFO) || this.level.equals(Log.WARNING)
				|| this.level.equals(Log.ERROR) || this.level.equals(Log.DEBUG)) {
			log(msg, ERROR, e);
		}

	}

	public synchronized void log(String msg, String type, Exception e) {
		try {
			String completeMsg = logFormat.format(new Date()) + "[" + type
					+ "]" + " - " + msg + " ";
			if (type.equals(Log.ERROR)) {
				stream.write(completeMsg.getBytes());
				stream.flush();
				// bos.write(completeMsg.getBytes());
				// bos.writeBytes(completeMsg);
				if (e != null) {
					e.printStackTrace(new PrintStream(stream, true));
					// e.printStackTrace();
				}
				stream.flush();
			} else {
				completeMsg += "\n";
				stream.write(completeMsg.getBytes());
				stream.flush();
				// dos.writeBytes(completeMsg);
				// dos.flush();
			}

		} catch (FileNotFoundException ex) {
			ex.printStackTrace();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	public static void main(String[] args) {
		Log logger = new FileLogger("terminal118Mobile", Log.DEBUG);
		logger.start();
		logger.info("INFO");
		logger.warn("WARNING", null);
		try {
			thrwExc();
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("ERROR", e);
		}
		logger.debug("DEBUG");
		logger.close();
	}

	private static void thrwExc() throws Exception {
		throw new Exception("TEST");
	}

	public void start() {
		try {
			completeFileName = fileName + "." + sdf.format(today) + ".log";
			stream = new FileOutputStream(completeFileName, true);

		} catch (FileNotFoundException e) {
			if (stream == null) {
				try {
					Thread.sleep(5000);
				} catch (InterruptedException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}
				try {
					stream = new FileOutputStream("/mnt/sdcard/emsmobile."
							+ sdf.format(today) + ".log", true);
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}

		}
		// dos = new DataOutputStream(stream);
		// bos = new ByteArrayOutputStream();

	}

	public void close() {
		try {
			stream.close();
		} catch (IOException e1) {
			// CREME Auto-generated catch block
			e1.printStackTrace();
		}
		// try {
		// bos.close();
		// }
		// catch (IOException e) {
		// // CREME Auto-generated catch block
		// e.printStackTrace();
		// }

	}

	public void reset() {
		close();
		start();

	}

	public void setLevel(String level) {
		this.level = level;

	}

	public void warn(String msg) {
		warn(msg, null);

	}

	public void error(String msg) {
		error(msg, null);

	}
}
